package com.ermace.robo.jumper.game.GameObjects;

import java.util.Random;

import com.ermace.robo.jumper.MainActivity;
import com.ermace.robo.jumper.game.GameView;
import com.ermace.robo.jumper.game.Line;
import com.ermace.robo.jumper.game.Player.Position;
import com.ermace.robo.jumper.game.Player.Robot;

import android.os.Parcel;
import android.os.Parcelable;


public class Meteorite extends Rectangle {
	
	
	/**
    *
    * This field is needed for Android to be able to
    * create new objects, individually or as arrays.
    *
    * This also means that you can use use the default
    * constructor to create the object and use another
    * method to hyrdate it as necessary.
    *
    */
	
	private double speed;
	private double resetTimer = 0;
	
	public static final Parcelable.Creator<Meteorite> CREATOR =
			new Parcelable.Creator<Meteorite>() {
		@Override
		public Meteorite createFromParcel(Parcel in) {
			return new Meteorite(in);
		}

		@Override
		public Meteorite[] newArray(int size) {
			return new Meteorite[size];
		}
	};
	
	
	public Meteorite(double length, double speed, int pic) {
		super(0, 0, length, pic);
		Random rand = new Random();
		double newX = -100 + (double) rand.nextInt((int)MainActivity.getScreenWidth());
		super.pxPosition = new Position(newX, 0);
		this.speed = speed;
		
	}
	
	public Meteorite(Parcel in) {
		super(in);
	}
	
	@Override
	public void update(double timeIntervalMS)
	{
		double interval = timeIntervalMS / 1000.0;
		resetTimer += interval;
		super.pxPosition = getPxPosition().add(new Position( speed * interval, speed * interval));
		if(resetTimer > 10)
		{
			
			Random rand = new Random();
			double newX = -100 + (double) rand.nextInt((int)MainActivity.getScreenWidth());
			resetTimer = rand.nextInt(7);
			super.pxPosition = new Position(newX, 0);
		}
	}
	
	@Override
	public void interact(Robot robot) {
		if(robot.getPosition().getDistanceTo(getPxPosition()) < getPxLength() + Robot.RADIUS_HORIZONTAL)
		{
			robot.kill();
			Random rand = new Random();
			super.pxPosition = new Position(-100 + (double) rand.nextInt((int)MainActivity.getScreenWidth()), 0);
		}
	}
	
}
